package systempen.cmanager;

import java.util.Calendar;

import systempen.cmanager.DialogAccountBrowser.IFoundAccount;
import systempen.cmanager.DialogTypeBrowser.IFoundCostType;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class BudgetAddNew extends FragmentActivity implements OnDateSetListener{
	public TextView _tvFrom;
	public TextView _tvTo; 
	public TextView _tvAccount;
	public TextView _tvPath;
	public TextView _tvCostCode;
	public TextView _tvCostCodeN;
	public String _account;
	public String _from_or_to;
	private static final int DATE_DIALOG_ID_from = 0;
	private static final int DATE_DIALOG_ID_to = 1;
	private Context _contekst;
	public void onCreate(Bundle icicle){
		super.onCreate(icicle);
		ThemePicker.onActivityCreateSetTheme(this);
		try{
			setContentView(R.layout.budget_add_new);
			_tvFrom = (TextView) findViewById(R.id.budget_add_new_from_text_view);
			_tvTo = (TextView) findViewById(R.id.budget_add_new_to_text_view);
			_tvAccount = (TextView) findViewById(R.id.budgetAddNewTextAccount);
			_tvPath = (TextView) findViewById(R.id.budget_add_new_textTypePathN);
			_tvCostCode = (TextView) findViewById(R.id.budget_add_new_costCode);
			_tvCostCodeN = (TextView) findViewById(R.id.budget_add_new_textCostCodeName);
			_tvFrom.setText(util.getDefaultFromDateView(getApplicationContext()));
			_tvTo.setText(util.gettMaxDayOfMonthView(getApplicationContext()));

			_tvCostCode.setText("0"); //initializaion
			_tvCostCodeN.setText("root");
			_tvPath.setText(" \\ ");
			_contekst = getParent();
			if (_contekst == null)
				_contekst = this;
			if (_account == null)
			{
				_account = util.getDefaultAccountCode(getApplicationContext());
			}
			
		}// od try
		catch (Exception ex) {
			Toast.makeText(this, "BudgetAddNew error: " + ex.getMessage(), Toast.LENGTH_LONG).show();
		}
	}
	public class DatePickerFragmentt extends DialogFragment{

		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			// Use the current date as the default date in the picker
			final Calendar c = Calendar.getInstance();
			int year = c.get(Calendar.YEAR);
			int month = c.get(Calendar.MONTH);
			int day = c.get(Calendar.DAY_OF_MONTH);

			// Create a new instance of DatePickerDialog and return it
			return new DatePickerDialog(getParent(), (BudgetAddNew) getParent(), year, month, day);
		}
		/*
		public void onDateSet(DatePicker view, int year, int month, int day) {
			// Do something with the date chosen by the user
			if (getTag().equals("datePickerFrom")){
				TextView tvFrom = (TextView)  getActivity().findViewById(R.id.budget_add_new_from_text_view);
				String dbDate = util.dbStringDateToViewStringDate(Integer.toString(year)+"-"+util.AddZeros(month+1)+"-"+util.AddZeros(day), getActivity().getApplicationContext());
				tvFrom.setText(dbDate);
			}else if (getTag().equals("datePickerTo")){
				TextView tvTo = (TextView) getActivity().findViewById(R.id.budget_add_new_to_text_view);
				String dbDate = util.dbStringDateToViewStringDate(Integer.toString(year)+"-"+util.AddZeros(month+1)+"-"+util.AddZeros(day), getActivity().getApplicationContext());
				tvTo.setText(dbDate);
			}
		}
		 */
	}
	public void onChangeType(View v){
		IFoundCostType ifo = new IFoundCostType() {

			@Override
			public void onFoundCostType(String costType) {
				_tvCostCode.setText(costType);
				_tvCostCodeN.setText(util.getLevelName(costType,getApplicationContext()));
				_tvPath.setText(util.getPathOfLevel1(costType, getApplicationContext()));
			}
		};
		DialogTypeBrowser dt = new DialogTypeBrowser(_contekst, "0", ifo);
		dt.show();	
	}
	public void onBudgetAddNewDateFrom(View v){
		try{
			showDialog(DATE_DIALOG_ID_from);
		} catch(Exception ex){
			util.showDialog(ex.getMessage().toString(), _contekst);
		}
	}

	public void onBudgetAddNewDateTo(View v){
		try{
			showDialog(DATE_DIALOG_ID_to);
		} catch(Exception ex){
			util.showDialog(ex.getMessage().toString(), _contekst);
		}
	}
	@Override
	protected Dialog onCreateDialog(int id) {
		Calendar c = Calendar.getInstance();
		int cyear = c.get(Calendar.YEAR);
		int cmonth = c.get(Calendar.MONTH);
		int cday = c.get(Calendar.DAY_OF_MONTH);
		switch (id) {
		case DATE_DIALOG_ID_from:
			return new DatePickerDialog(_contekst,  mDateSetListener,  cyear, cmonth, cday);
		case DATE_DIALOG_ID_to:
			return new DatePickerDialog(_contekst,  mDateSetListener2,  cyear, cmonth, cday);
		}
		return null;
	}
	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
		// onDateSet method
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			try{
				//String date_selected = String.valueOf(monthOfYear+1)+" /"+String.valueOf(dayOfMonth)+" /"+String.valueOf(year);
				String date_selected = String.valueOf(year)+"-"+util.AddZeros(monthOfYear+1)+"-"+util.AddZeros(dayOfMonth);

				Toast.makeText(_contekst, "Selected Date is ="+date_selected, Toast.LENGTH_SHORT).show();
				_tvFrom.setText(util.dbStringDateToViewStringDate(date_selected, getApplicationContext()));
			}
			catch(Exception ex){
				util.showDialog(ex.getMessage().toString(), _contekst);
			}	
		}
	};
	private DatePickerDialog.OnDateSetListener mDateSetListener2 = new DatePickerDialog.OnDateSetListener() {
		// onDateSet method
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			try{
				String date_selected = String.valueOf(year)+"-"+util.AddZeros(monthOfYear+1)+"-"+util.AddZeros(dayOfMonth);
				Toast.makeText(_contekst, "Selected Date is ="+date_selected, Toast.LENGTH_SHORT).show();
				_tvTo.setText(util.dbStringDateToViewStringDate(date_selected, getApplicationContext()));
			}
			catch(Exception ex){
				util.showDialog(ex.getMessage().toString(), _contekst);
			}	
		}
	};

	public void onChooseAccountForBudget(View v){
		IFoundAccount ifa = new IFoundAccount() {

			@Override
			public void onFoundAccount(String account) {
				// TODO Auto-generated method stub
				_account = account;
				_tvAccount.setText(util.getAccountName(account, getApplicationContext()));
			}
		};
		DialogAccountBrowser dab = new DialogAccountBrowser(_contekst, ifa);
		dab.show();


	}
	public void onSave (View v){
		try{
			String sqlInsert;
			String cost_type, date_from, date_to, account, description, amount;
			cost_type = _tvCostCode.getText().toString();
			if (cost_type.equals("")){
				cost_type = "0";
			}
			date_from = util.viewStringDateToDBStringDate(_tvFrom.getText().toString(), getApplicationContext());
			date_to = util.viewStringDateToDBStringDate(_tvTo.getText().toString(), getApplicationContext());
			account = _account;// ((TextView)findViewById(R.id.budget_add_new_budgetCode)).getText().toString();
			if(account.equals("")){
				account = "0";
			}
			description = ((EditText)findViewById(R.id.textDescriptionBudgetN)).getText().toString();
			amount = ((EditText)findViewById(R.id.BudgetAddNewAmount)).getText().toString();
			if(amount.equals("")||(amount == "0")){
				Toast.makeText(this, "Budget must be greater than 0", Toast.LENGTH_LONG).show();
				return;
			}
			sqlInsert = "insert into budget (cost_type,date_from,date_to, account, description, value) values "+ 
					"("+cost_type+",\'"+date_from+"\',\'"+date_to+"\',"+account+",\'"+description+"\',"+amount+")";
			util.execSQL(sqlInsert, getApplicationContext());
			finish();
			//Intent intent = new Intent(this, BudgetList.class);
			//startActivity(intent);
		}
		catch (Exception ex){
			Toast.makeText(this, "BudgetAddNew save error: " + ex.getMessage(), Toast.LENGTH_LONG).show();			
		}
	}
	public void onCancel(View v){
		try{
			Intent intent = new Intent(this, BudgetList.class);
			startActivity(intent);
		}
		catch (Exception ex){
			Toast.makeText(this, "BudgetAddNew save error: " + ex.getMessage(), Toast.LENGTH_LONG).show();			
		}
	}
	@Override //pressed back button cya
	public boolean onKeyDown(int keyCode, KeyEvent event)  {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			startActivity(new Intent(this, BudgetList.class)); 
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	@Override
	public void onDateSet(DatePicker view, int year, int month, int day) {
		// TODO Auto-generated method stub
		// Do something with the date chosen by the user
		if (_from_or_to.equals("from")){
			TextView tvFrom = (TextView)  getParent().findViewById(R.id.budget_add_new_from_text_view);
			String dbDate = util.dbStringDateToViewStringDate(Integer.toString(year)+"-"+util.AddZeros(month+1)+"-"+util.AddZeros(day), getParent().getApplicationContext());
			tvFrom.setText(dbDate);
		}else if (_from_or_to.equals("to")){
			TextView tvTo = (TextView) getParent().findViewById(R.id.budget_add_new_to_text_view);
			String dbDate = util.dbStringDateToViewStringDate(Integer.toString(year)+"-"+util.AddZeros(month+1)+"-"+util.AddZeros(day), getParent().getApplicationContext());
			tvTo.setText(dbDate);
		}
	}
}


